﻿using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Security.Cryptography;


namespace Jy123.Common
{
    public class Input
    {
        /// <summary>
        /// MD5加密字符串处理
        /// </summary>
        /// <param name="Half">加密是16位还是32位；如果为true为16位</param>
        public static string MD5(string Input, bool Half)
        {
            string output = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(Input, "MD5").ToLower();
            if (Half) output = output.Substring(8, 16);
            return output;
        }

        public static string MD5(string Input)
        {
            return MD5(Input, true);
        }

        /// <summary>
        /// URL地址编码
        /// </summary>
        public static string URLEncode(string Input)
        {
            if (string.IsNullOrEmpty(Input)) return string.Empty;
            else
                return HttpContext.Current.Server.UrlEncode(Input);
        }
        /// <summary>
        /// 截取字符串函数
        /// </summary>
        public static string GetSubString(string Str, int Num)
        {
            string sdot = string.Empty;
            if (Str == null || Str == "")
                return string.Empty;
            string outstr = string.Empty;
            int n = 0;
            foreach (char ch in Str)
            {
                n += System.Text.Encoding.Default.GetByteCount(ch.ToString());
                if (n > Num)
                {
                    sdot = "..";
                    break;
                }
                else
                {
                    outstr += ch;
                }
            }
            if (Str.Length > 2)
            {
                return outstr + sdot;
            }
            else
            {
                return outstr;
            }
        }

        /// <summary>
        /// 检测输入是否是整型
        /// </summary>
        /// <param name="Input">输入的字符串</param>
        /// <returns>true/false</returns>
        public static bool IsInteger(string Input)
        {
            if (Input == null) { return false; }
            else { return IsInteger(Input); }
        }
                
        /// <summary>
        /// 过滤字符串中的html代码
        /// </summary>
        /// <param name="strHtml"></param>
        /// <returns>返回过滤之后的字符串</returns>
        public static string LostHTML(string strHtml)
        {
            string[] aryReg ={   
                      @"<script[^>]*?>.*?</script>",    
                      @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",   
                      @"([\r\n])[\s]+",   
                      @"&(quot|#34);",   
                      @"&(amp|#38);",   
                      @"&(lt|#60);",   
                      @"&(gt|#62);",     
                      @"&(nbsp|#160);",     
                      @"&(iexcl|#161);",   
                      @"&(cent|#162);",   
                      @"&(pound|#163);",   
                      @"&(copy|#169);",   
                      @"&#(\d+);",   
                      @"-->",   
                      @"<!--.*\n"   
                    };

            string[] aryRep =   {   
                        "",   
                        "",   
                        "",   
                        "\"",   
                        "&",   
                        "<",   
                        ">",   
                        "   ",   
                        "\xa1",//chr(161),   
                        "\xa2",//chr(162),   
                        "\xa3",//chr(163),   
                        "\xa9",//chr(169),   
                        "",   
                        "\r\n",   
                        ""   
                      };

            string newReg = aryReg[0];
            string strOutput = strHtml;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }
            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            return strOutput;
        }
    }
}
